﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WebInfo.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the WebInfo type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Collaboris.Saf.Actions.Wss.Entities
{
    using System;
    using System.Xml.Serialization;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.Entities;

    using Tracing;

    using Utils;

    [CollaborisClass(
          Title = "Web Application Info",
          ReleaseStatus = ReleaseStatus.Beta,
          DisableDocumentation = false,
          ContactAuthorEmail = "Support@Collaboris.co.uk",
          Summary = "Defines the parameters to create a new web application in SharePoint."
         )]
    [Serializable]
    public class WebApplicationInfo : WSSEntityBase
    {
        private bool? overwrite;

        private int? port;

        private string appPoolId;

        private string appPoolUserName;

        private string appPoolPassword;

        private bool? createNewDatabase;

        private string databaseName;

        private string databaseServer;

        private bool? useNtlm;

        private bool? allowAnonymous;

        private string hostHeader;

        private bool? useSsl;

        private string rootDirectory;

        private string searchServer;

        private string name;

        private bool? deleteContentDatabases;


        [CollaborisProperty(
           Summary = "Specifies whether to overwrite the web application if it already exists at the destination.",
           Title = "Overwrite",
           Example = "true",
           Required = false,
           InputHint = InputHint.NullableBoolean)]
        [XmlElement(IsNullable = true)]
        public bool? Overwrite
        {
            get
            {
                return overwrite;
            }
            set
            {
                overwrite = value;
            }
        }

        [CollaborisProperty(
           Summary = "Specifies the IIS port on which to create the web application.",
           Title = "Overwrite",
           Example = "4433",
           Required = false,
           InputHint = InputHint.NullableNumber)]
        [XmlElement(IsNullable = true)]
        public int? Port
        {
            get
            {
                return port;
            }
            set
            {
                port = value;
            }
        }

        [CollaborisProperty(
            Summary = "Specifies the app pool ID for the new web application.",
            Title = "App Pool Id",
            Example = "",
            Required = false,
            InputHint = InputHint.SingleText)]
        [XmlAttribute]
        public string AppPoolId
        {
            get
            {
                return appPoolId;
            }
            set
            {
                appPoolId = value;
            }
        }

        [CollaborisProperty(
            Summary = "Specifies the account under which the app pool will run for the new web application.",
            Title = "App Pool User Name",
            Example = @"${USERDOMAIN}\${USERNAME}",
            Required = false,
            InputHint = InputHint.SingleText)]
        [XmlAttribute]
        public string AppPoolUserName
        {
            get
            {
                return appPoolUserName;
            }
            set
            {
                appPoolUserName = value;
            }
        }

        [CollaborisProperty(
            Summary = "Specifies the password for the account under which the app pool will run for the new web application.",
            Title = "App Pool Password",
            Example = "MyPassword",
            Required = false,
            InputHint = InputHint.SingleText)]
        [XmlAttribute]
        public string AppPoolPassword
        {
            get
            {
                return appPoolPassword;
            }
            set
            {
                appPoolPassword = value;
            }
        }

        [CollaborisProperty(
           Summary = "Specifies whether to create a new database for the web application.",
           Title = "Create New Database",
           Example = "false",
           Required = false,
           Remarks = @"If this is set to 'True' then 'DatabaseName' and 'DatabaseServer' can be set, but are ignored.",
           InputHint = InputHint.Boolean)]
        [XmlElement(IsNullable = true)]
        public bool? CreateNewDatabase
        {
            get
            {
                return createNewDatabase;
            }
            set
            {
                createNewDatabase = value;
            }
        }

        [CollaborisProperty(
            Summary = "Specifies the database name under which the new web application will be created.",
            Title = "Database Name",
            Example = "",
            Required = false,
            InputHint = InputHint.SingleText)]
        [XmlAttribute]
        public string DatabaseName
        {
            get
            {
                return databaseName;
            }
            set
            {
                databaseName = value;
            }
        }

        [CollaborisProperty(
            Summary = "Specifies the database server under which the new web application will be created.",
            Title = "App Pool User Name",
            Example = "",
            Required = false,
            InputHint = InputHint.SingleText)]
        [XmlAttribute]
        public string DatabaseServer
        {
            get
            {
                return databaseServer;
            }
            set
            {
                databaseServer = value;
            }
        }

        [CollaborisProperty(
                   Summary = "Specifies whether to user NTLM authentication for the web application.",
                   Title = "User NTLM",
                   Example = "",
                   Required = false,
                   InputHint = InputHint.Boolean)]
        [XmlElement(IsNullable = true)]
        public bool? UseNTLM
        {
            get
            {
                return useNtlm;
            }
            set
            {
                useNtlm = value;
            }
        }

        [CollaborisProperty(
           Summary = "Specifies whether to allow anonymous access to the web application.",
           Title = "Allow anonymous",
           Example = "true",
           Required = false,
           InputHint = InputHint.Boolean)]
        [XmlElement(IsNullable = true)]
        public bool? AllowAnonymous
        {
            get
            {
                return allowAnonymous;
            }
            set
            {
                allowAnonymous = value;
            }
        }

        [CollaborisProperty(
            Summary = "Specifies a host header under which the new web application will be created.",
            Title = "Host Header",
            Example = "",
            Required = false,
            InputHint = InputHint.SingleText)]
        [XmlAttribute]
        public string HostHeader
        {
            get
            {
                return hostHeader;
            }
            set
            {
                hostHeader = value;
            }
        }

        [CollaborisProperty(
           Summary = "Specifies whether to use (secured sockets) SSL for the web application.",
           Title = "Use SSL",
           Example = "false",
           Required = false,
           InputHint = InputHint.Boolean)]
        [XmlElement(IsNullable = true)]
        public bool? UseSSL
        {
            get
            {
                return useSsl;
            }
            set
            {
                useSsl = value;
            }
        }

        [CollaborisProperty(
           Summary = "Specifies the root directory under which the new web application will be created.",
           Title = "Root Directory",
           Example = "",
           Required = false,
           InputHint = InputHint.SingleText)]
        [XmlAttribute]
        public string RootDirectory
        {
            get
            {
                return rootDirectory;
            }
            set
            {
                rootDirectory = value;
            }
        }

        /// <summary>
        /// Gets or sets the search server.
        /// </summary>
        /// <value>The search server.</value>
        //public string SearchServer
        //{
        //    get
        //    {
        //        return searchServer;
        //    }
        //    set
        //    {
        //        searchServer = value;
        //    }
        //}

        [CollaborisProperty(
            Summary = "Specifies the web app name under which the new web application will be created.",
            Title = "Web App name",
            Example = "SAF New Web App",
            Required = true,
            InputHint = InputHint.SingleText)]
        [XmlAttribute]
        public string WebAppName
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        [CollaborisProperty(
           Summary = "Specifies whether to delete the content databases for the web application (during an undo).",
           Title = "Delete Content Databases",
           Example = "true",
           Required = false,
           Remarks = @"If during an undo the web application gets removed specify 'True' to remove the content databases.",
           InputHint = InputHint.Boolean)]
        [XmlElement(IsNullable = true)]
        public bool? DeleteContentDatabases
        {
            get
            {
                return deleteContentDatabases;
            }
            set
            {
                deleteContentDatabases = value;
            }
        }

        /// <summary>
        /// Gets the URL based on various settigns in the Entity.
        /// </summary>
        /// <returns></returns>
        public string Url
        {
            get
            {
                TraceHelper.TraceMethodStart(ActionsTraceHelper.TraceSwitch, "WebApplicationInfo", "GetUrl");

                string url = string.Empty;

                if (!this.UseSSL.HasValue)
                    this.useSsl = false;

                if (!this.Port.HasValue)
                    this.Port = 0;


                if (this.UseSSL.Value)
                {
                    url = "https://";
                }
                else
                {
                    url = "http://";
                }

                if (string.IsNullOrEmpty(this.HostHeader))
                {
                    url = url + EnvironmentHelper.GetCurrentMachineName();
                }
                else
                {
                    url = url + this.hostHeader;
                }

                if (this.Port > 0 && this.Port != 80)
                {
                    url = url + ":" + this.Port;
                }

                TraceHelper.TraceMethodEnd(ActionsTraceHelper.TraceSwitch, "WebApplicationInfo", "GetUrl");
                return url;
            }
        }
    }
}
